﻿using AIVision.Inspect.Halcon;
using AIVision.Model.Events;
using AIVision.Model.InspectModels;
using AIVision.UI.ViewModels;
using DryIoc;
using HalconDotNet;
using HandyControl.Controls;
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static System.Collections.Specialized.BitVector32;

namespace AIVision.UI.Views
{
    /// <summary>
    /// CameraView.xaml 的交互逻辑
    /// </summary>
    public partial class CameraView : UserControl
    {
        string[] colors = new string[21]{"green" ,"blue","yellow" ,"pink" ,"orange" ,"slate blue","magenta" ,"spring green" ,"orange red" ,"red"
            ,"cyan" ,"coral" ,"cadet blue" ,"light gray","medium slate blue","gray","dim gray"  ,"white" ,"black" ,"dark olive green" ,"forest green" };
        public CameraView(IEventAggregator eventAggregator)
        {
            InitializeComponent();
            eventAggregator.GetEvent<ShowImageEvent>().Subscribe(ShowImageMethod, ThreadOption.UIThread);
        }

        private void ShowImageMethod(ShowImageModel arg)
        {
            if (arg.SourceImage == null)
                return;
            switch (arg.HSmartWindowName)
            {
                case "Hsmart1":
                    ShowImg(Hsmart1, arg);
                    break;
                case "Hsmart2":
                    ShowImg(Hsmart2, arg);
                    break;
                case "Hsmart3":
                    ShowImg(Hsmart3, arg);
                    break;
                case "Hsmart4":
                    ShowImg(Hsmart4, arg); ;
                    break;
            }
           
        }

        private void ShowImg(HSmartWindowControlWPF window, ShowImageModel show)
        {
            CommonHelper.set_display_font(window.HalconWindow, 48, "mono", "true", "false");
            HOperatorSet.ClearWindow(window.HalconWindow);
            if (show.InspectImage == null)
            {
                HTuple ImageHeight, ImageWidth;
                HOperatorSet.GetImageSize(show.SourceImage, out ImageWidth, out ImageHeight);
                HOperatorSet.SetPart(window.HalconWindow, 0, 0, ImageHeight - 1, ImageWidth - 1);
                HOperatorSet.SetDraw(window.HalconWindow, "margin");
                HOperatorSet.DispObj(show.SourceImage, window.HalconWindow);
                window.SetFullImagePart();
            }
            else
            {
                HTuple ImageHeight, ImageWidth;
                HOperatorSet.GetImageSize(show.InspectImage, out ImageWidth, out ImageHeight);
                HOperatorSet.SetPart(window.HalconWindow, 0, 0, ImageHeight - 1, ImageWidth - 1);
                HOperatorSet.SetDraw(window.HalconWindow, "margin");
                HOperatorSet.DispObj(show.InspectImage, window.HalconWindow);
                window.SetFullImagePart();

                if(show.Defects.Count>0)
                {
                    int i = 0;
                    foreach (var defect in show.Defects)
                    {
                        HOperatorSet.SetColor(window.HalconWindow, colors[i]);
                        if (defect.Value != null)
                            HOperatorSet.DispObj(defect.Value, window.HalconWindow);
                        i++;
                    }
                }
            }
            if (show.Score != 0)
            {
                HOperatorSet.SetColor(window.HalconWindow, "green");
                HOperatorSet.WriteString(window.HalconWindow, $"{show.Location}  OK");
            }
            else
            {
                HOperatorSet.SetColor(window.HalconWindow, "red");
                HOperatorSet.WriteString(window.HalconWindow, $"{show.Location}  NG");
            }
        }
    }
}
